//
//  HomeViewController.m
//  DIsciplineSelectPartner
//
//  Created by Lizzy on 15/3/7.
//  Copyright (c) 2015年 Lizzy. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()<KDCycleBannerViewDataource,KDCycleBannerViewDelegate,MJRefreshBaseViewDelegate>
{
    
    MJRefreshHeaderView *_header; //集成下拉刷新
    
    NSMutableArray *imagesArray;
}

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [NSThread sleepForTimeInterval:1.5];   //设置进程停止1.5秒
    
//    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//    NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
//    [self setTitle: name];
    
    // 设置返回按钮文字
    // Set this in every view controller so that the back button displays back instead of the root view controller name
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    //改变TabBar字体颜色
    [self.tabBarController.tabBar setSelectedImageTintColor:NAVCOLOR];
    
    _scrollView.contentSize = CGSizeMake(WIDTH, 950);
    
    //集成下拉菜单
    _header = [MJRefreshHeaderView header];
    _header.scrollView = _scrollView;
    _header.delegate = self;
    
    //Banner图片轮播展示
    _cycleBannerViewBottom = [KDCycleBannerView new];
    _cycleBannerViewBottom.frame = CGRectMake(0, 0, KWIDTH, 160);
    _cycleBannerViewBottom.datasource = self;
    _cycleBannerViewBottom.delegate = self;
    _cycleBannerViewBottom.continuous = YES;
    _cycleBannerViewBottom.autoPlayTimeInterval = 5;
    [_scrollView addSubview:_cycleBannerViewBottom];
    
    //我的选科君图标
    UIBarButtonItem *xkjImageLeftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"wdxkj"] style:UIBarButtonItemStylePlain target:self action:nil];
    [xkjImageLeftButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = xkjImageLeftButton;
    
    //我的选科君按钮
    UIBarButtonItem *loginButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xkjrightbtn"] style:UIBarButtonItemStylePlain target:self action:@selector(login)];
    [loginButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = loginButton;
    


}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self refresh];
    
    [self showHomeView];
}

-(void)refresh{
    
    //动态获取轮播图片
    [self useDefaultImg];
    
}

-(void)showHomeView{
    
    //兴趣推荐
    _interestRecommandView.frame = CGRectMake(0, 160, WIDTH, 155);
    _interestImageView_1.image = [UIImage imageNamed:@"interest_1"];
    _interestImageView_1.layer.masksToBounds=YES;
    _interestImageView_1.layer.cornerRadius=_interestImageView_1.frame.size.width/2;
    _interestImageView_1.layer.borderWidth= 2.0;
    _interestImageView_1.layer.borderColor = [UIColor orangeColor].CGColor;
    _interestTitleLabel_1.text = [NSString stringWithFormat:@"动漫"];
    
    UITapGestureRecognizer *interestImageTapGesture_1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    interestImageTapGesture_1.numberOfTapsRequired = 1;
    interestImageTapGesture_1.enabled =YES;
    [interestImageTapGesture_1 delaysTouchesBegan];
    [interestImageTapGesture_1 cancelsTouchesInView];
    [_interestImageView_1 addGestureRecognizer:interestImageTapGesture_1];

    
    _interestImageView_2.image = [UIImage imageNamed:@"interest_2"];
    _interestImageView_2.layer.masksToBounds=YES;
    _interestImageView_2.layer.cornerRadius=_interestImageView_2.frame.size.width/2;
    _interestImageView_2.layer.borderWidth= 2.0;
    _interestImageView_2.layer.borderColor = [UIColor orangeColor].CGColor;
    _interestTitleLabel_2.text = [NSString stringWithFormat:@"机器人"];
    UITapGestureRecognizer *interestImageTapGesture_2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    interestImageTapGesture_2.numberOfTapsRequired = 1;
    interestImageTapGesture_2.enabled =YES;
    [interestImageTapGesture_2 delaysTouchesBegan];
    [interestImageTapGesture_2 cancelsTouchesInView];
    [_interestImageView_2 addGestureRecognizer:interestImageTapGesture_2];
    
    _interestImageView_3.image = [UIImage imageNamed:@"interest_3"];
    _interestImageView_3.layer.masksToBounds=YES;
    _interestImageView_3.layer.cornerRadius=_interestImageView_2.frame.size.width/2;
    _interestImageView_3.layer.borderWidth= 2.0;
    _interestImageView_3.layer.borderColor = [UIColor orangeColor].CGColor;
    _interestTitleLabel_3.text = [NSString stringWithFormat:@"小说"];
    UITapGestureRecognizer *interestImageTapGesture_3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    interestImageTapGesture_3.numberOfTapsRequired = 1;
    interestImageTapGesture_3.enabled =YES;
    [interestImageTapGesture_3 delaysTouchesBegan];
    [interestImageTapGesture_3 cancelsTouchesInView];
    [_interestImageView_3 addGestureRecognizer:interestImageTapGesture_3];
    
    //职业看看
    _professionLookView.frame = CGRectMake(0, _interestRecommandView.frame.origin.y +_interestRecommandView.frame.size.height+5, WIDTH, 250);
     _professionImageView_1.image = [UIImage imageNamed:@"profession_1"];
     _professionTitleLabel_1.text = [NSString stringWithFormat:@"教师"];
     _professionImageView_2.image = [UIImage imageNamed:@"profession_2"];
     _professionTitleLabel_2.text = [NSString stringWithFormat:@"律师"];
     _professionImageView_3.image = [UIImage imageNamed:@"profession_3"];
     _professionTitleLabel_3.text = [NSString stringWithFormat:@"医生"];
    _professionImageView_4.image = [UIImage imageNamed:@"profession_4"];
    _professionTitleLabel_4.text = [NSString stringWithFormat:@"软件工程师"];
    UITapGestureRecognizer *professionImageTapGesture_1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    professionImageTapGesture_1.numberOfTapsRequired = 1;
    professionImageTapGesture_1.enabled =YES;
    [professionImageTapGesture_1 delaysTouchesBegan];
    [professionImageTapGesture_1 cancelsTouchesInView];
    [_professionImageView_1 addGestureRecognizer:professionImageTapGesture_1];
    UITapGestureRecognizer *professionImageTapGesture_2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    professionImageTapGesture_2.numberOfTapsRequired = 1;
    professionImageTapGesture_2.enabled =YES;
    [professionImageTapGesture_2 delaysTouchesBegan];
    [professionImageTapGesture_2 cancelsTouchesInView];
    [_professionImageView_2 addGestureRecognizer:professionImageTapGesture_2];
    UITapGestureRecognizer *professionImageTapGesture_3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    professionImageTapGesture_3.numberOfTapsRequired = 1;
    professionImageTapGesture_3.enabled =YES;
    [professionImageTapGesture_3 delaysTouchesBegan];
    [professionImageTapGesture_3 cancelsTouchesInView];
    [_professionImageView_3 addGestureRecognizer:professionImageTapGesture_3];
    UITapGestureRecognizer *professionImageTapGesture_4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    professionImageTapGesture_4.numberOfTapsRequired = 1;
    professionImageTapGesture_4.enabled =YES;
    [professionImageTapGesture_4 delaysTouchesBegan];
    [professionImageTapGesture_4 cancelsTouchesInView];
    [_professionImageView_4 addGestureRecognizer:professionImageTapGesture_4];
    
    
    //--------------------------------------------------
    // 职业看看的面板点击   Edit by William 2015/03/19
    UITapGestureRecognizer *jobCategoryViewTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jobCategoryViewTapGestureViewTouch:)];
    jobCategoryViewTapGesture.numberOfTapsRequired = 1;
    jobCategoryViewTapGesture.enabled =YES;
    [jobCategoryViewTapGesture delaysTouchesBegan];
    [jobCategoryViewTapGesture cancelsTouchesInView];
    [_professionLookView addGestureRecognizer:jobCategoryViewTapGesture];
    //--------------------------------------------------/
    
    //大学专业
    _universityProfessionalView.frame = CGRectMake(0, _professionLookView.frame.origin.y +_professionLookView.frame.size.height+5, WIDTH, 250);
    _universityProfessionalView.userInteractionEnabled = YES; // Edit by William
    _universityImageView_1.image = [UIImage imageNamed:@"university_1"];
    _universityImageView_1.userInteractionEnabled = YES; // Edit by William
    _universityTitleLabel_1.text = [NSString stringWithFormat:@"浙江大学"];
    _universityImageView_2.image = [UIImage imageNamed:@"university_2"];
    _universityImageView_2.userInteractionEnabled = YES; // Edit by William
    _universityTitleLabel_2.text = [NSString stringWithFormat:@"中国美术学院"];
    _universityImageView_3.image = [UIImage imageNamed:@"university_3"];
    _universityImageView_3.userInteractionEnabled = YES; // Edit by William
    _universityTitleLabel_3.text = [NSString stringWithFormat:@"浙江工业大学"];
    _universityImageView_4.image = [UIImage imageNamed:@"university_4"];
    _universityImageView_4.userInteractionEnabled = YES; // Edit by William
    _universityTitleLabel_4.text = [NSString stringWithFormat:@"杭州电子科技大学"];
    
    //--------------------------------------------------
    // 大学专业的面板点击   Edit by William 2015/03/13
    UITapGestureRecognizer *universityProfessionalViewTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(universityProfessionalViewTouch:)];
    universityProfessionalViewTapGesture.numberOfTapsRequired = 1;
    universityProfessionalViewTapGesture.enabled =YES;
    [universityProfessionalViewTapGesture delaysTouchesBegan];
    [universityProfessionalViewTapGesture cancelsTouchesInView];
    [_universityProfessionalView addGestureRecognizer:universityProfessionalViewTapGesture];
    //--------------------------------------------------/
    
    UITapGestureRecognizer *universityImageTapGesture_1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    universityImageTapGesture_1.numberOfTapsRequired = 1;
    universityImageTapGesture_1.enabled =YES;
    [universityImageTapGesture_1 delaysTouchesBegan];
    [universityImageTapGesture_1 cancelsTouchesInView];
    [_universityImageView_1 addGestureRecognizer:universityImageTapGesture_1];
    
    UITapGestureRecognizer *universityImageTapGesture_2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    universityImageTapGesture_2.numberOfTapsRequired = 1;
    universityImageTapGesture_2.enabled =YES;
    [universityImageTapGesture_2 delaysTouchesBegan];
    [universityImageTapGesture_2 cancelsTouchesInView];
    [_universityImageView_2 addGestureRecognizer:universityImageTapGesture_2];
    
    UITapGestureRecognizer *universityImageTapGesture_3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    universityImageTapGesture_3.numberOfTapsRequired = 1;
    universityImageTapGesture_3.enabled =YES;
    [universityImageTapGesture_3 delaysTouchesBegan];
    [universityImageTapGesture_3 cancelsTouchesInView];
    [_universityImageView_3 addGestureRecognizer:universityImageTapGesture_3];
    
    UITapGestureRecognizer *universityImageTapGesture_4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouch:)];
    universityImageTapGesture_4.numberOfTapsRequired = 1;
    universityImageTapGesture_4.enabled =YES;
    [universityImageTapGesture_4 delaysTouchesBegan];
    [universityImageTapGesture_4 cancelsTouchesInView];
    [_universityImageView_4 addGestureRecognizer:universityImageTapGesture_4];
    
    //选科方案
    _subjectsSelectView.frame = CGRectMake(0, _universityProfessionalView.frame.origin.y +_universityProfessionalView.frame.size.height+5, WIDTH, 110);
    _subjectsButtonView_1.titleLabel.text = [NSString stringWithFormat:@"物化生"];
    _subjectsButtonView_1.backgroundColor = [UIColor orangeColor];
    _subjectsButtonView_1.layer.masksToBounds=YES;
    _subjectsButtonView_1.layer.cornerRadius= 3;
    _subjectsButtonView_2.titleLabel.text = [NSString stringWithFormat:@"政史地"];
    _subjectsButtonView_2.backgroundColor = [UIColor orangeColor];
    _subjectsButtonView_2.layer.masksToBounds=YES;
    _subjectsButtonView_2.layer.cornerRadius= 3;
    
}

-(void)login{

}


//--------------------------------------------------------
// Created by William

// 点击职业看看面板（jobCategoryView）的手势处理函数
-(void)jobCategoryViewTapGestureViewTouch:(UIGestureRecognizer*) recognizer
{
    
    [self performSegueWithIdentifier:@"jobCategory" sender:self];
    
}
// 点击大学专业面板（universityProfessionalView）的手势处理函数
-(void)universityProfessionalViewTouch:(UIGestureRecognizer*) recognizer
{
    
    [self performSegueWithIdentifier:@"universityProfessional" sender:self];
    
}

//---------------------------------------------------------/

//--------------------------------------------------------
// Created by William 2015/03/10
 
// 点击图片的手势处理函数
-(void)imageTouch:(UIGestureRecognizer*) recognizer
{
//    NSLog(@"你点击了");
//    NSLog(@"你点击的地方在：%@",  NSStringFromCGPoint([recognizer locationInView:[recognizer.view superview]]));
    
    //------------------------------------
    // 测试 Bmob
    //查找Job表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Job"];
    //查找Job表里面id为bmlTZXXe的数据
    [bquery getObjectInBackgroundWithId:@"CsMBpWWw" block:^(BmobObject *object,NSError *error){
        if (error){
            //进行错误处理
            NSLog(@"查询错误");
        }else{
//            NSLog(@"查询到了");
            //表里有id为CsMBpWWw的数据
            if (object) {
                //得到name
                NSString *name = [object objectForKey:@"name"];
                NSLog(@"%@",name);
            }
        }
    }];
    //------------------------------------/
}

//---------------------------------------------------------/

-(void)useDefaultImg
{
    imagesArray = [NSMutableArray array];
    [imagesArray addObject:[UIImage imageNamed:@"banner_1"]];
    [imagesArray addObject:[UIImage imageNamed:@"defaultHeader"]];
    
    [_cycleBannerViewBottom reloadDataWithCompleteBlock:^{
        
        //数据加载完成后结束下拉刷新
        [_header endRefreshing];
        
    }];
}

#pragma mark - KDCycleBannerViewDataource

- (NSArray *)numberOfKDCycleBannerView:(KDCycleBannerView *)bannerView {
    
    return imagesArray;
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    
    // 创建一个bitmap的context
    
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(newsize);
    
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}

- (UIViewContentMode)contentModeForImageIndex:(NSUInteger)index {
    return UIViewContentModeScaleToFill;
}

- (UIImage *)placeHolderImageOfZeroBannerView {
    return [UIImage imageNamed:@"defaultHeader"];
}

#pragma mark - KDCycleBannerViewDelegate

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didScrollToIndex:(NSUInteger)index{
    // NSLog(@"didScrollToIndex:%ld", (long)index);
}

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didSelectedAtIndex:(NSUInteger)index{
    
//    NSDictionary * dic = homePicturesData[index];
//    
//    if ([dic objectForKey:@"linkUrl"] != nil && ![[dic objectForKey:@"linkUrl"] isEqualToString:@""])
//    {
//        self.hidesBottomBarWhenPushed = YES;
//        [self setBackButton];
//        HomePictureDetailViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"homePictureDetailViewController"];
//        vc.imgUrlStr = [dic objectForKey:@"linkUrl"];
//        [self.navigationController pushViewController:vc animated:YES];
//        self.hidesBottomBarWhenPushed = NO;
//    }
    
}

#pragma mark - 刷新的代理方法
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    [self refresh];
}


@end
